package com.ruoyi.web.controller.account;

import com.ruoyi.account.domain.Expense;
import com.ruoyi.account.service.ExpenseService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// src/main/java/com/ruoyi/account/controller/
@RestController
@RequestMapping("/api/expense")
@Anonymous
public class ExpenseController {

    @Autowired
    private ExpenseService expenseService;

    @PostMapping
    public R<Void> addExpense(@RequestBody Expense expense) {
        expenseService.addExpense(expense);
        return R.ok();
    }

    @GetMapping("/debts")
    @Anonymous
    public R<Map<Long, BigDecimal>> getDebts(@RequestParam Long ledgerId) {
        return R.ok(expenseService.calculateDebts(ledgerId));
    }

    /**
     * 获取账本总支出
     */
    @GetMapping("/summary")
    @Anonymous
    public AjaxResult getTotalExpense(@RequestParam Long ledgerId) {
        BigDecimal total = expenseService.getTotalExpense(ledgerId);
        // 1. 获取总支出

        // 2. 获取最近支出记录
        Expense expense = new Expense();
        expense.setLedgerId(ledgerId);
        List<Expense> recentExpenses = expenseService.getListExpense(expense);

        // 3. 封装响应
        Map<String, Object> data = new HashMap<>();
        data.put("totalExpense", total);
        data.put("recentExpenses", recentExpenses);
        return AjaxResult.success(data);
    }
}